import numpy as np %not needed

def F1_n0(irr, kedf)

copied from ProFit file
as simple version of a PSI efficiency (F1) irradiance model 
with no intersystem electron transport chain
the term kedf is the same as the k'epsilon' parameter in the
paper and talk 

partA = 1(kedfirr)
output =(1(1+partA))
return output
def F1_n1(irr, kedf, keq)

copied from Profit
irr is irradiance
this is a model of PSI efficiency (F1) versus irradiance 
with one electron transport donor buffering
P700 oxidation 
the term kedf is the same as the k'epsilon' parameter in the
paper and talk 

partA = 1((kedfirr)(1+kedfirr))
partB = 1 + ((kedfirr)keq)
partC = partA  partB
partD = 1(1+partC)
return partD
def F1_PVV_n2_with_Keq_COMB_py(x, Keq = 4, keDf = 100)

this is a model for two donors per P700
it also has some reasonable default values of Keq and keDf
parameter x is the irradiance
keDf is the lumped parameter keepsilon in paper

K1= 2Keq
K2=0.5Keq
partA = 1((keDfx)(1 + (keDfx) + (keDfx)2))
partB = 1+ ( ((keDfx)K2) + ((keDfx)2K1) )
partC = partA  partB
partD = 1(1+partC)
return partD
    
def F1_n3(irr, kedf, keq)

copied from Profit

K1= Keq3
K2= Keq1
K3= Keq0.333
partA = 1((kedfirr)(1 + (kedfirr) + (kedfirr)2 + (kedfirr)3))
partB = 1+ ( ((kedfirr)K3) + ((kedfirr)2K2) + ((kedfirr)3K1) )
partC = partA  partB
partD = 1(1+partC)
return partD
    
def F1_n3_combination_Keq(irr, kedf, keq)

copied from Profit
same as module above!

K1= keq3
K2= keq1
K3= keq0.333
partA = 1((kedfirr)(1 + (kedfirr) + (kedfirr)2 + (kedfirr)3))
partB = 1+ ( ((kedfirr)K3) + ((kedfirr)2K2) + ((kedfirr)3K1) )
partC = partA  partB
partD = 1(1+partC)
return partD

def J1_n2_comb(irr,kedf,keq)


flux rather than efficiency
modified from the following m file

function output = j1_PVV_n2_with_Keq(irr,kedf,keq,)

inputs irradiance, a kedf param and a keq param
calculates flux version of the PVV n=2 model with combination
approach to adjusting Keq
this PVV model (function) is for PSI flux (ie irradiance times efficiency) 
good values are 
kedf 50
keq 5


K1= 2Keq
K2=0.5Keq
partA=1(kedfirr(1+kedfirr+(kedfirr)2))
partB=1+((kedfirr)(partK2))+(((kedfirr)2)(partK1))
partC=partApartB
output= irr(1(1+partC))
return output

def J1_n3(irr, kedf, keq)

copied from Profit
inputs irradiance, a kedf param and a keq param
calculates flux version of the PVV n=3 model with combination
approach to adjusting Keq
this PVV model (function) is for PSI flux (ie irradiance times efficiency) 
good values are 
kedf 50
keq 5

K1= Keq3
K2= Keq1
K3= Keq0.333
partA = 1((kedfirr)(1 + (kedfirr) + (kedfirr)2 + (kedfirr)3))
partB = 1+ ( ((kedfirr)K3) + ((kedfirr)2K2) + ((kedfirr)3K1) )
partC = partA  partB
partD = irr(1(1+partC))
return partD


def F2_n2(irr,kedf=164.6,keq=24.01,C=0.77)


from the following m file
function output = F2_PVV_n2_with_Keq(irr,kedf,keq,C)

calculates PVV n=2 model with the combination framework of the original PVV model 
this PVV model (function) has is adjusted for PSII efficiency in that it
has an extra parameter not present in PSI F1 model which adjusts the 0 - 1
range of the PVV F1 model (ie for PSI) to the lower range found for F2 data
this parameter is simply a scaling factor, and this function can also be 
used to fit FCO2irr curves though in this case C will not be 0.75 - 0.8
but will be lower - more like 0.05.
typical values from JA260793
kedf - 164.6
keq - 24.01
C - 0.770 (not fitting dark adapted FvFm)
see Usersjeremyharbinson1DocumentsProjectsPVV_modelOct_2010_tests_Padraic_paperPVV_n2_Keq_func_fit_JA260793_F2_660.plot


partK1= 2Keq
partK2=K2=0.5Keq
partA=1(kedfirr(1+kedfirr+(kedfirr)2))
partB=1+((kedfirr)(partK2))+(((kedfirr)2)(partK1))
partC=partApartB
output= C(1(1+partC))
return output

def F2_n3(irr,kedf,keq,C)


from the following m file

function output = j2_PVV_n2_with_Keq(irr,kedf,keq,C)

calculates PVV n=3 model with the combination framework used to adjust Keq of 
the original PVV model 
this PVV model (function) is modified for PSII efficiency in that it
has an extra parameter not present in PSI F1 model which adjusts the 0 - 1
range of the PVV F1 model (ie for PSI) to the lower range found for F2 data
this parameter is simply a scaling factor, and this function can also be 
used to fit FCO2irr curves though in this case C will not be 0.75 - 0.8
but will be lower - more like 0.05.

for CO2 fixn of JA260793 xums660 and COFIX660
parameters       Standard deviations
kedf = 102.8055       'Delta'a[1] = 3.2756
Keq = 13.7951       'Delta'a[2] = 9.0406
C =  4.6543e-2   'Delta'a[3] = 2.0649e-3




K1= Keq3
K2= Keq1
K3= Keq0.333
partA = 1((kedfirr)(1 + (kedfirr) + (kedfirr)2 + (kedfirr)3))
partB = 1+ ( ((kedfirr)K3) + ((kedfirr)2K2) + ((kedfirr)3K1) )
partC = partA  partB
partD = C(1(1+partC))
return partD

def J2_n3(irr,kedf,keq,C)


from the following m file

function output = j2_PVV_n2_with_Keq(irr,kedf,keq,C)

calculates PVV n=2 model with improved Keq function 
ie not use the combination framework or the original PVV model but a
function describing the equilibrium based on Keq
this PVV model (function) has is adjusted for PSII efficiency in that it
has an extra parameter not present in PSI F1 model which adjusts the 0 - 1
range of the PVV F1 model (ie for PSI) to the lower range found for F2 data
this parameter is simply a scaling factor, and this function can also be 
used to fit FCO2irr curves though in this case C will not be 0.75 - 0.8
but will be lower - more like 0.05.

for CO2 fixn of JA260793 xums660 and COFIX660
parameters       Standard deviations
kedf = 102.8055       'Delta'a[1] = 3.2756
Keq = 13.7951       'Delta'a[2] = 9.0406
C =  4.6543e-2   'Delta'a[3] = 2.0649e-3




K1= Keq3
K2= Keq1
K3= Keq0.333
partA = 1((kedfirr)(1 + (kedfirr) + (kedfirr)2 + (kedfirr)3))
partB = 1+ ( ((kedfirr)K3) + ((kedfirr)2K2) + ((kedfirr)3K1) )
partC = partA  partB
partD = Cirr(1(1+partC))
return partD
def JCO2_PVV_n2_with_Keq_COMB_py(x, Keq = 4, keDf = 100, C=0.07)
    
    model for fitting a CO2 fixation irradiance curve with 2 donors
    keDf is the lumped parameter keepsilon in paper'
    modified to fit CO2 fixation efficiency efficiency irradiance data
    so it has a 'C' term  - the light-limited quantum yield for 
CO2 fixation
this is function is basically the same as that for PSII efficiency which also needs a 'C'
term 
reasonable values are given
    imported from Profit
    
    K1= 2Keq
    K2= 0.5Keq
    partA = 1((keDfx)(1 + (keDfx) + (keDfx)2))
    partB = 1+ ( ((keDfx)K2) + ((keDfx)2K1) )
    partC = partA  partB
    partD = Cx(1(1+partC))
    return partD

def JCO2_PVV_n3_with_Keq_COMB_py(x, Keq = 4, keDf = 100, C=0.07)

old Profit file def F1_n3_combination_Keq(irr, kedf, keq)
model for fitting a CO2 fixation irradiance curve with 2 donors
    keDf is the lumped parameter keepsilon in paper'
    modified to fit CO2 fixation efficiency irradiance data
    so it has a 'C' term  - the light-limited quantum yield for 
CO2 fixation
this is function is basically the same as that for PSII efficiency which also needs a 'C'
term 
'x' is irradiance - the same as 'irr' used elsewhere
reasonable values are given
copied from Profit

K2= Keq1
K3= Keq0.333
K1= 3Keq
partA = 1((keDfx)(1 + (keDfx) + (keDfx)2 + (keDfx)3))
partB = 1+ ( ((keDfx)K3) + ((keDfx)2K2) + ((keDfx)3K1) )
partC = partA  partB
partD = Cx(1(1+partC))
return partD
def JA260793_f1_irr()

specimen data XF1 from JA260793
xums_660
1.00000
19.51000
33.93000
59.16500
85.43000
135.90000
208.00000
292.46000
375.89000
457.77500

f1_660
1.00000
0.97600
0.97510
0.94290
0.90480
0.81780
0.68400
0.56870
0.48580
0.41430


irr=np.array([[1.00000],
[19.51000],
[33.93000],
[59.16500],
[85.43000],
[135.90000],
[208.00000],
[292.46000],
[375.89000],
[457.77500]])

f1=np.array([[1.00000],
[0.97600],
[0.97510],
[0.94290],
[0.90480],
[0.81780],
[0.68400],
[0.56870],
[0.48580],
[0.41430]])
return(irr.flatten(),f1.flatten())
def JA260793_f2_irr()

specimen data XF2 from JA260793
xums_660
1.00000
19.51000
33.93000
59.16500
85.43000
135.90000
208.00000
292.46000
375.89000
457.77500
F2BG_660
0.80680
0.76710
0.75000
0.71850
0.67210
0.59180
0.46070
0.36360
0.29220
0.25000


irr=np.array([[1.00000],
[19.51000],
[33.93000],
[59.16500],
[85.43000],
[135.90000],
[208.00000],
[292.46000],
[375.89000],
[457.77500]])

f2=np.array([[0.80680],
[0.76710],
[0.75000],
[0.71850],
[0.67210],
[0.59180],
[0.46070],
[0.36360],
[0.29220],
[0.25000]])
return(irr.flatten(),f2.flatten())

def JA260793_CO2_irr()

specimen data XCO2 from JA260793 min values CO2
set to 1 and 0.05
xums_660
1.00000
19.51000
33.93000
59.16500
85.43000
135.90000
208.00000
292.46000
375.89000
457.77500
COFIX660
0.05000
0.94190
1.40070
2.39090
2.84970
3.59840
3.96060
4.25040
4.39530
4.34700


irr=np.array([[1.00000],
[19.51000],
[33.93000],
[59.16500],
[85.43000],
[135.90000],
[208.00000],
[292.46000],
[375.89000],
[457.77500]])

CO2=np.array([[0.05000],
[0.94190],
[1.40070],
[2.39090],
[2.84970],
[3.59840],
[3.96060],
[4.25040],
[4.39530],
[4.34700]])
return(irr.flatten(),CO2.flatten())
def JA260793_J2_irr()

specimen data irrJ2 from JA260793 min values irr and J2
set to 1 and 0.1
xums_660
1.00000
19.51000
33.93000
59.16500
85.43000
135.90000
208.00000
292.46000
375.89000
457.77500
J2_660
0.10000
14.96660
25.44750
42.51110
57.42020
80.43060
95.83250
106.34910
109.83800
114.44370


irr=np.array([[1.00000],
[19.51000],
[33.93000],
[59.16500],
[85.43000],
[135.90000],
[208.00000],
[292.46000],
[375.89000],
[457.77500]])

J2=np.array([[0.10000],
[14.96660],
[25.44750],
[42.51110],
[57.42020],
[80.43060],
[95.83250],
[106.34910],
[109.83800],
[114.44370]])
return(irr.flatten(),J2.flatten())

def JA260793_J1_irr()

specimen data J1irr from JA260793 min values irr and J1
set to 1 and 1
xums_660
1.00000
19.51000
33.93000
59.16500
85.43000
135.90000
208.00000
292.46000
375.89000
457.77500
J1
1.00000
19.04100
33.08530
55.78410
77.29380
111.13320
142.26420
166.32800
182.62580
189.64960


irr=np.array([[1.00000],
[19.51000],
[33.93000],
[59.16500],
[85.43000],
[135.90000],
[208.00000],
[292.46000],
[375.89000],
[457.77500]])

J1=np.array([[1.00000],
[19.04100],
[33.08530],
[55.78410],
[77.29380],
[111.13320],
[142.26420],
[166.32800],
[182.62580],
[189.64960]])
return(irr.flatten(),J1.flatten())



def F1_PVV_n3_with_Keq_COMB_py(x, Keq = 4, keDf = 100)

old Profit file dF1_PVV_n3_with_Keq_comb_py(x, Keq = 3, keDf = 400)
'keDf is the lumped parameter keepsilon in paper'

K1= Keq3
K2= Keq1
K3= Keq0.333
partA = 1((keDfx)(1 + (keDfx) + (keDfx)2 + (keDfx)3))
partB = 1+ ( ((keDfx)K3) + ((keDfx)2K2) + ((keDfx)3K1) )
partC = partA  partB
partD = 1(1+partC)
return partD

def Res_F1_PVV_n3_with_Keq_COMB_py(x, params, data)

old Profit file dF1_PVV_n3_with_Keq_comb_py(x, Keq = 3, keDf = 400)
modified to work as a residuals (Res) calculator as needed by the Scipy
Optimise routine
'keDf is the lumped parameter keepsilon in paper'

Keq=params['Keq'].value
keDf=params['Kedf'].value
K1= Keq3
K2= Keq1
K3= Keq0.333
partA = 1((keDfx)(1 + (keDfx) + (keDfx)2 + (keDfx)3))
partB = 1+ ( ((keDfx)K3) + ((keDfx)2K2) + ((keDfx)3K1) )
partC = partA  partB
partD = 1(1+partC)
return data-partD